package com.cskaoyan.service;

import com.cskaoyan.bean.configInfo.ExperssVo;
import com.cskaoyan.bean.configInfo.MallVo;
import com.cskaoyan.bean.configInfo.OrderVo;
import com.cskaoyan.bean.configInfo.WxVo;
import com.cskaoyan.mapper.MarketSystemMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * 创建日期: 2022/05/05 20:44
 * @author wangyonghong
 */
@Service
public class ConfigServiceImpl implements ConfigService{
    @Autowired
   MarketSystemMapper marketSystemMapper;
    @SneakyThrows
    @Override
    public MallVo query() {
        return   (MallVo)getVo( "com.cskaoyan.bean.configInfo.MallVo");
    }
    @SneakyThrows
    @Override
    public void  update(MallVo mallVo) {
        updatePo(mallVo);
    }
    @SneakyThrows
    @Override
    public ExperssVo queryExpress() {
        return (ExperssVo)getVo( "com.cskaoyan.bean.configInfo.ExperssVo");
    }
    @SneakyThrows
    @Override
    public void updateExpress(ExperssVo experssVo) {
        updatePo(experssVo);
    }
    @SneakyThrows
    @Override
    public OrderVo queryOrder() {
        return (OrderVo)getVo("com.cskaoyan.bean.configInfo.OrderVo");
    }
    @SneakyThrows
    @Override
    public void updateOrder(OrderVo orderVo) {
        updatePo(orderVo);
    }
    @SneakyThrows
    @Override
    public WxVo queryWx() {
        return (WxVo) getVo("com.cskaoyan.bean.configInfo.WxVo"); }
    @SneakyThrows
    @Override
    public void updateWx(WxVo wxVo) {
        updatePo(wxVo);
    }

    private Object getVo(String ref) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException {
        Class personCls = Class.forName(ref);
        Constructor declaredConstructor = personCls.getDeclaredConstructor();
        Object o = declaredConstructor.newInstance();
        Field[] declaredFields = personCls.getDeclaredFields();
        for (Field f:declaredFields) {
            f.setAccessible(true);
            f .set(o,marketSystemMapper.selectByPrimaryName(f.getName()));
        }
        return o;
    }
    private void updatePo(Object object) throws  NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException {
        String str = getDate();
        Class aClass = object.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        Constructor declaredConstructor = aClass.getDeclaredConstructor(aClass);
        declaredConstructor.setAccessible(true);
        Object o = declaredConstructor.newInstance(object);
        for (Field f:declaredFields) {
            Method method = aClass.getMethod("get"+ f.getName().substring(0, 1).toUpperCase()+f.getName().substring(1));
            method.setAccessible(true);
            marketSystemMapper.update( f.getName() , (String)method.invoke(o),str);
        }
    }

     private String getDate() {
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }
}
